package com.example.firstapp;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.firstapp.databinding.ActivitySqliteBinding;
import com.example.firstapp.utils.Constants;
import com.example.firstapp.utils.DatabaseHelper;

public class SqliteActivity extends AppCompatActivity {

    DatabaseHelper databaseHelper;
    private SQLiteDatabase db;
//    EditText et_sqlite_name, et_sqlite_email;
    private ActivitySqliteBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);
        binding = ActivitySqliteBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        databaseHelper = new DatabaseHelper(this);
        db = databaseHelper.getWritableDatabase();
//        findView();
    }

    private void findView() {
//        et_sqlite_name = findViewById(R.id.et_sqlite_name);
//        et_sqlite_email = findViewById(R.id.et_sqlite_email);
//        textView1 = findViewById(R.id.activity_sharedperference_textview1);
//        textView1.setOnClickListener(this);
    }

    /**
     * 新增用户
     */
    public void addUser(View view) {
        ContentValues values = getInputData();
        if (values != null){
            db.insert(Constants.TABLE_NAME,null,values);
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
            // 清空输入框
            clearInputData();
        }
    }

    // 清空输入框
    private void clearInputData() {
        binding.etSqliteName.setText("");
        binding.etSqliteEmail.setText("");
    }

    private ContentValues getInputData() {
        String name = binding.etSqliteName.getText().toString();
        String email = binding.etSqliteEmail.getText().toString();
        if (name.isEmpty() || email.isEmpty()){
            Toast.makeText(this, "输入的用户名和邮箱不能为空!", Toast.LENGTH_SHORT).show();
            return null;
        }
        ContentValues values = new ContentValues();
        // 注意：名字需要DatabaseHelper建表的字段名一致
        values.put("name",name);
        values.put("email",email);
        return values;
    }


    /**
     * 新增用户:根据用户名，删除用户
     */
    public void delUser(View view) {
        String name = binding.etSqliteName.getText().toString();
        if (name.isEmpty()){
            Toast.makeText(this, "输入的用户名为空!", Toast.LENGTH_SHORT).show();
            return;
        }
        db.delete(Constants.TABLE_NAME,"name=?",new String[]{name});
        Toast.makeText(this, "删除用户: "+ name +" 成功", Toast.LENGTH_SHORT).show();
        clearInputData();
    }
    /**
     * 修改用户: 根据用户名，更新email
     */
    public void editUser(View view) {
        ContentValues values = getInputData();
        if (values != null){
            String name = binding.etSqliteName.getText().toString();
            // 注意：名字需要DatabaseHelper建表的字段名一致
            db.update(Constants.TABLE_NAME,values,"name=?",new String[]{name});
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
            clearInputData();
        }
    }

    /**
     * 查询用户
     */
    @SuppressLint("Range")
    public void queryUser(View view) {
        binding.tvSqliteContent.setText("");
        // 查询所有用户
         Cursor cursor = db.query(Constants.TABLE_NAME, null, null, null, null, null, null);
        if (cursor.moveToFirst()){
            do {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String email = cursor.getString(cursor.getColumnIndex("email"));
                Log.d("TAG", "name: " + name + " email: " + email);
                binding.tvSqliteContent.append("name: " + name + " email: " + email + "\n");
            }while (cursor.moveToNext());
        }
    }
}